package com.rtx.edi.platform.application.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.edi.model.application.po.PlApplicationLinkDtl;
import com.rtx.edi.platform.application.mapper.PlApplicationLinkDtlMapper;
import com.rtx.edi.platform.application.mapper.PlApplicationLinkMapper;
import com.rtx.edi.platform.application.service.PlApplicationLinkDtlService;

/**
 * <p>
 * 应用关联明细表  服务实现类
 * </p>
 *
 * @author wq
 * @since 2023-03-14
 */
@Service
public class PlApplicationLinkDtlServiceImpl extends ServiceImpl<PlApplicationLinkDtlMapper, PlApplicationLinkDtl> implements PlApplicationLinkDtlService {

    @Autowired
    private PlApplicationLinkMapper plApplicationLinkMapper;

    @Override
    public void selectPlApplicationLinkDtlByLinkId(Page<PlApplicationLinkDtl> pageParam, PlApplicationLinkDtl plApplicationLinkDtl) {
        LambdaQueryWrapper<PlApplicationLinkDtl> query = new LambdaQueryWrapper<>();
        query.orderByAsc(PlApplicationLinkDtl::getId)
                .eq(PlApplicationLinkDtl::getLinkId,plApplicationLinkDtl.getLinkId());
        baseMapper.selectPage(pageParam,query);
    }

    @Override
    public void deletePlApplicationLinkDtlById(PlApplicationLinkDtl plApplicationLinkDtl) {
        //根据应用关联id获取明细总数
        LambdaQueryWrapper<PlApplicationLinkDtl> query = new LambdaQueryWrapper<>();
        query.eq(PlApplicationLinkDtl::getLinkId,plApplicationLinkDtl.getLinkId());
        Long count = baseMapper.selectCount(query);
        if(count == 1){
            //只有一条明细，删除表头
            plApplicationLinkMapper.deleteById(plApplicationLinkDtl.getLinkId());
        }
        baseMapper.deleteById(plApplicationLinkDtl.getId());
    }
}
